/**
 * MessageAction.java
 * Created at 2018-2-23
 * Created by zhanghailin
 * Copyright (C) 2018 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.message.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.utils.StringUtils;
import com.broadtext.message.model.SysMessage;
import com.broadtext.message.service.IMessageService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>ClassName: MessageAction</p>
 * <p>Description: 消息控制层</p>
 * <p>Author: zhanghailin</p>
 * <p>Date: 2018-2-23</p>
 */
@Controller
@RequestMapping("/main/message")
public class MessageAction {
    
    @Autowired
    @Qualifier("messageService")
    private IMessageService messageService;
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param request
     * @param response
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/getMessage.mvc")
    @ResponseBody
    public Map<String,Object> getMessage(HttpServletRequest request, HttpServletResponse response)  {
        PageBounds pageBounds = new PageBounds();
        //解析请求参数，将String字符串转为map
        Map<String,Object> map = new HashMap<String,Object>();
        String aoData = request.getParameter("aoData");
        JSONArray array = JSONArray.parseArray(aoData);
        if (array!=null) {
            Iterator<Object> it = array.iterator();
            while (it.hasNext()) {
                JSONObject obj;
                obj = (JSONObject) it.next();
                if (obj.get("value") != null && StringUtils.isNotBlank(obj.get("value")+"")) {
                    map.put(obj.get("name") + "", obj.get("value"));
                }
            }
        }
        String receiver = String.valueOf(request.getSession().getAttribute("sys.user"));
        Map<String, Object> dataMap = new HashMap<String, Object>();
        if(StringUtils.isBlank(receiver)) {
            return dataMap;
        }
        map.put("receiver", receiver);
        //分页查询条数
        pageBounds.setLimit(Integer.parseInt(map.get("iDisplayLength").toString()));
        //分页第几页
        pageBounds.setPage(Integer.parseInt(map.get("iDisplayStart").toString()) / pageBounds.getLimit() + 1);
        try {
            EntityPageBean<SysMessage> pageBean = this.messageService.findEntityPageByQuery(map, pageBounds);
            dataMap.put("recordsTotal", pageBean.getiTotalRecords());
            dataMap.put("recordsFiltered", pageBean.getiTotalRecords());
            dataMap.put("aaData", pageBean.getResults());
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return dataMap;
    }
    
    /**
     * 
     * <p>Description: 获取消息提醒</p>
     * @param request
     * @return
     */
    @RequestMapping(value = "/getMessageById.mvc")
    @ResponseBody
    public Map<String, Object> getMessageById(Long pkId) {
        Map<String, Object> data = new HashMap<String, Object>();
        SysMessage message = new SysMessage();
        try {
            message = this.messageService.getMessageById(pkId);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        data.put("message", message);
        return data;
    }
    
    /**
     * 
     * <p>Description: 获取消息提醒</p>
     * @param request
     * @return
     */
    @RequestMapping(value = "/unBindAppClientId.mvc")
    @ResponseBody
    public Map<String, Object> unBindAppClientId(String loginName,String clientId) {
        Map<String, Object> data = new HashMap<String, Object>();
        try {
            this.messageService.unBindAppClientId(loginName,clientId);
            data.put("msg", "SUCCESS");
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return data;
    }
    
    /**
     * <p>
     * Description: 删除
     * </p>
     * 
     * @param request 请求
     * @param pkId id
     * @param response 返回对象
     */
    @RequestMapping(value = "/deleteMessage.mvc")
    @ResponseBody
    public Map<String, Object> deleteMessage(HttpServletRequest request,String pkId) {
        Map<String, Object> data = new HashMap<String, Object>();
        //数组 存储待删除主键id
        String[] idArray = pkId.split(",");
        Long[] pkIdArray = new Long[]{};
        if(idArray.length>0) {
            pkIdArray = new  Long[idArray.length];
            for (int i = 0; i < idArray.length; i++) {
                pkIdArray[i] = Long.valueOf(idArray[i]);
            }
        }
        try {
            this.messageService.deleteMessage(pkIdArray);
            data.put("msg", "SUCCESS");
        } catch (DaoException e) {
            data.put("msg", "FAILURE");
            e.printStackTrace();
        }
        return data;
    }
    
    /**
     * <p>
     * Description: 删除
     * </p>
     * 
     * @param request 请求
     * @param pkId id
     * @param response 返回对象
     */
    @RequestMapping(value = "/readAllMessage.mvc")
    @ResponseBody
    public Map<String, Object> readAllMessage(HttpServletRequest request) {
        Map<String, Object> data = new HashMap<String, Object>();
        String receiver = String.valueOf(request.getSession().getAttribute("sys.user"));
        if(StringUtils.isBlank(receiver)) {
            return data;
        }
        try {
            this.messageService.readAllMessage(receiver);
            data.put("msg", "SUCCESS");
        } catch (DaoException e) {
            data.put("msg", "FAILURE");
            e.printStackTrace();
        }
        return data;
    }
    
    /**
     * <p>
     * Description: 删除
     * </p>
     * 
     * @param request 请求
     * @param pkId id
     * @param response 返回对象
     */
    @RequestMapping(value = "/readMessage.mvc")
    @ResponseBody
    public Map<String, Object> readMessage(HttpServletRequest request,String pkId) {
        Map<String, Object> data = new HashMap<String, Object>();
        //数组 存储待删除主键id
        String[] idArray = pkId.split(",");
        Long[] pkIdArray = new Long[]{};
        if(idArray.length>0) {
            pkIdArray = new  Long[idArray.length];
            for (int i = 0; i < idArray.length; i++) {
                pkIdArray[i] = Long.valueOf(idArray[i]);
            }
        }
        try {
            this.messageService.readMessage(pkIdArray);
            data.put("msg", "SUCCESS");
        } catch (DaoException e) {
            data.put("msg", "FAILURE");
            e.printStackTrace();
        }
        return data;
    }
    
    /**
     * 
     * <p>Description: 获取消息提醒</p>
     * @param request
     * @return
     */
    @RequestMapping(value = "/getMessageNotification.mvc")
    @ResponseBody
    public Map<String, Object> getMessageNotification(HttpServletRequest request) {
        Map<String, Object> data = new HashMap<String, Object>();
        String receiver = String.valueOf(request.getSession().getAttribute("sys.user"));
        if(StringUtils.isBlank(receiver)) {
            return data;
        }
        List<Object> messageList = new ArrayList<Object>();
        Long count = new Long(0);
        try {
            messageList = this.messageService.getMessageNotification(receiver);
            count = this.messageService.getMessageCount(receiver);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        data.put("messageList", messageList);
        data.put("count", count);
        return data;
    }

}
